package com.liming.auth.controller;

import com.liming.auth.service.MenuService;
import com.liming.common.pojo.dto.authinfo.AddOrUpdateMenuDTO;
import com.liming.common.pojo.vo.authinfo.MenuVO;
import com.liming.common.result.LMResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author liming
 * @date 2025/1/14
 */
@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "后台权限信息-Service")
@RequestMapping("/service/menu")
public class MenuMircoController {

    private final MenuService menuService;

    @Operation(summary = "树 - 菜单树")
    @GetMapping
    public LMResult<List<MenuVO>> tree() {
        List<MenuVO> result = menuService.tree();
        return LMResult.success(result);
    }

    @Operation(summary = "树 - 角色对应菜单权限")
    @GetMapping("role/{id}")
    public LMResult<List<Long>> roleMenu(@PathVariable Long id) {
        List<Long> result = menuService.menuIdsByRoleId(id);
        return LMResult.success(result);
    }

    @Operation(summary = "新增 - 菜单")
    @PostMapping
    public LMResult<Boolean> add(@RequestBody AddOrUpdateMenuDTO addOrUpdateMenuDTO) {
        Boolean result = menuService.saveMenu(addOrUpdateMenuDTO);
        return LMResult.success(result);
    }

    @Operation(summary = "更新 - 菜单")
    @PutMapping
    public LMResult<Boolean> update(@RequestBody AddOrUpdateMenuDTO addOrUpdateMenuDTO) {
        Boolean result = menuService.updateMenu(addOrUpdateMenuDTO);
        return LMResult.success(result);
    }

    @Operation(summary = "更新 - 角色菜单权限")
    @PutMapping("role/{id}")
    public LMResult<Boolean> updRoleMenu(@PathVariable Long id, @RequestBody List<Long> menuIds) {
        Boolean result = menuService.updRoleMenu(id, menuIds);
        return LMResult.success(result);
    }

    @Operation(summary = "删除 - 菜单")
    @DeleteMapping("{id}")
    public LMResult<Boolean> delete(@PathVariable Long id) {
        Boolean result = menuService.removeMenuWithChildrenById(id);
        return LMResult.success(result);
    }
}